package co.za.itweb.itwebafrica;



import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Surface;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import android.widget.RelativeLayout;

@SuppressLint("SetJavaScriptEnabled")
public class Banner extends Fragment {
	View myFragmentView;
	WebView myWebView;
	 String 	size ;
	 String orientation;
	 String screenResolution ;
	 int width;
	 int height;
	 String catId;
	 String mainfolder="";
	 private WebView banner;
	  String density="";
	
	int bannerHolderWidth;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setRetainInstance(true);
		DisplayMetrics metrics = new DisplayMetrics();
		getActivity().getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		 width = (int) metrics.widthPixels;

		 if(width <= 250){
			 mainfolder="240";
			
		
			 
		 }else if(width >250 && width <= 320){
			 mainfolder="320";
			
		 }else if(width>320 && width <= 480){
			 mainfolder="480";
			 
		 }else if(width>480 && width <= 720){
			 mainfolder="720";
		 }else if(width>720 && width <= 800){
			 mainfolder="800";
		 }else if(width>800 && width <= 1280){
			 mainfolder="1280";
		 }else{
			 mainfolder="1280";
		 }
		
		 Bundle extras = getArguments();
		  catId=  extras.getString("catID");
		 

		  
	}
	
	public String determineDensity(){
		switch (getResources().getDisplayMetrics().densityDpi) {
		case DisplayMetrics.DENSITY_LOW:
			density="ldpi";
			
		    break;
		case DisplayMetrics.DENSITY_MEDIUM:
			density= "mdpi";
			
			 break;
		case DisplayMetrics.DENSITY_HIGH:
			density= "hdpi";
			
			 break;
		case DisplayMetrics.DENSITY_XHIGH:
			density= "xdpi";
			
			 break;
		}
		return density;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	
		//setRetainInstance(true);
		myFragmentView = inflater.inflate(R.layout.fragmentbanner, container,
				false);
		return myFragmentView;
		
	}
	
	
	@Override
	public void onStart(){
		super.onStart();
		if(determineDensity()=="ldpi"){
			bannerHolderWidth=(int) (width*1.3);
		}else if(determineDensity()=="mdpi"){
			bannerHolderWidth=(int) (width);
		}else if(determineDensity()=="hdpi"){
			bannerHolderWidth=(int) (width)-(width/3);
		}else if(determineDensity()=="xdpi"){
			bannerHolderWidth=(int) (width)-(width/2);
		}
		
//	if(bannerHolderWidth > 728){
//		bannerHolderWidth=728;
//		}
		
		
		
					RelativeLayout linLayout = (RelativeLayout) getView().findViewById(R.id.webbanner);
					
					//if(width>=728){	
	    RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
	    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
	    banner = new WebView(getActivity()); 
		linLayout.addView(banner, layoutParams);
		/*
	}else{
		RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 1);
		banner = new WebView(getActivity()); 
		linLayout.addView(banner, layoutParams);
	}
					
			*/	
					

		 banner.getSettings().setJavaScriptEnabled(true);

		  banner.setWebChromeClient(new WebChromeClient() {

      	 });
		
		 banner.setWebViewClient(new WebViewClient(){
			 @Override
			    public boolean shouldOverrideUrlLoading(WebView view, String url) {
				 if (url != null && url.startsWith("http://")) {
			            view.getContext().startActivity(
			                new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
			            return true;
			        } else {
			            return false;
			        }
//				  Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse(url));
//			    	startActivity(viewIntent); 
			    	//super.shouldOverrideUrlLoading(view, url);
			       
			    }
			
			
			
			
			 
		 });
		banner.loadUrl("http://itweb.mobi/__bottombar/index.php?app=itwebafricalite&device=android&width="+mainfolder+"&height=50&density="+determineDensity()+"&section="+catId);
		// banner.loadData("<html><head><meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" /></head><body style=\"margin:0 auto;padding:0px;\"><iframe style=\"width:"+bannerHolderWidth+"px;height:50px;border:0px;margin:0 auto;\" src=\"http://itweb.mobi/__bottombar/index.php?app=defenceweb&device=android&width="+mainfolder+"&height=50&density="+determineDensity()+"&section="+catId+"\"></iframe></body></html>","text/html",null);

	}
	

	
	
	
		    	
				
}
